/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Pinjam;

import Koneksi.Koneksi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import kembali.kembali;

public class PinjamKontrol {

    private Connection conn;
    private int kuotabuku;
// setter dan getter

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public int getKuotabuku() {
        return kuotabuku;
    }

    public void setKuotabuku(int kuotabuku) {
        this.kuotabuku = kuotabuku;
    }

    public PinjamKontrol(Connection koneksi) {
        this.conn = koneksi;
    }
//fungsi method ini untuk koneksi database

    public static PinjamKontrol getKoneksiPinjam() throws SQLException {
        Koneksi konek = new Koneksi();
        PinjamKontrol kon = new PinjamKontrol(konek.getConnection());
        return kon;
    }
//fungsi method ini untuk memasukkan data pada database pinjam jika mahasiswa meminjam buku

    public void PinjamData(Data dt) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "insert into pinjam "
                    + "(judulBuku,Nim)"
                    + "values (?,?)";

            statement = conn.prepareStatement(sql);
            statement.setString(1, dt.getJudulBuku());
            statement.setString(2, dt.getNIM());
//            statement.setString(3, dt.getTanggalPinjam());
//            statement.setString(4, dt.getTanggalKembali());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.cancel();
                }
            } catch (SQLException exception) {
                throw exception;
            }


        }
    }

    public Data cariberdasrkanKode(String NIM) throws SQLException {
        PreparedStatement stat = null;
//        String rusult = "";
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);
//            String sql = "{CALL spCariKode(?,?)}";
//            stat = conn.prepareStatement("select namaBarang, kodeBarang, kategori, qtyMin, beliSetelah, satuan"
//                    + "from Barang"
//                    + "where kodeBarang= ?");
            stat = conn.prepareStatement("Select *"
                    + "From pinjam "
                    + "where NIM = ?");


            stat.setString(1, NIM);
            result = stat.executeQuery();
            Data data = null;
//            if (result.next()){
//            barang = new Barang();
//            Kategori kat = new Kategori();
//            barang.setKodeBarang(result.getString("kodeBarang"));//kodeBarang
//            barang.setNamaBarang(result.getString("namaBarang"));
//            String kodeKategori = result.getString(1); //2
//            kat.setKodeKategori(result.getString(3));//3
            if (result.next()) {
                data = new Data();
                data.setJudulBuku(result.getString(1));
                data.setNIM(result.getString(2));





            }
//           DataBarang.setBelisetelah(result.getString("belisetelah");

            conn.commit();
            return data;
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;

        } finally {
            try {
                conn.setAutoCommit(true);
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

    public List<Data> lihatseluruhKategori() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        try {
            conn.setAutoCommit(false);
            statement = conn.prepareStatement("Select *"
                    + "from pinjam order by judulBuku");
            result = statement.executeQuery();
            List<Data> kategoris = new ArrayList<Data>();
            while (result.next()) {
                kembali kategori = new kembali();
                kategori.setJudulBuku(result.getString("judulBuku"));
//                kategori.setTanggalPinjam(result.getString("tanggalPinjam"));
//                kategori.;

//                Data pinjam = new Data();
//                pinjam.setNIM(result.getString("NIM"));
//                kategoris.add(kategori);

            }
            conn.commit();
            return kategoris;
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement == null) {
                    statement.close();

                }
            } catch (SQLException exception) {
                throw exception;
            }
        }

    }

    public void kuota(Data data) throws SQLException {

        String kode = data.getJudulBuku();
        String sql = "update buku "
                + "set kuota = kuota -1 where judulBuku = '" + kode + "'";
        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
        conn.close();
    }
//      public void kuotaNOl(Data data) throws SQLException {
//
//        String kode = data.getJudulBuku();
//        String sql = "select * from buku "
//                + "where kuota = 0 order by judulBuku";
//       
//        JOptionPane.showConfirmDialog(null, "data kosong");
//         Statement stat = conn.createStatement();
//        stat.executeUpdate(sql);
//        conn.close();
//    }
}
